package com.syt.zdlb.controller;

import com.syt.commer.Result;
import com.syt.entity.model.cmn.Dict;
import com.syt.zdlb.service.DictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Api(description = "数据字典接口")
@RestController
@RequestMapping("/syt/zdlb/dict")
//@CrossOrigin// 跨域  使用了网关就不需要这个了
public class DictController {

    @Autowired
    private DictService dictService;// 数据字典的 service



    // 根据 dickCode 获取下级节点
    @ApiOperation(value = "根据 dickCode 获取下级节点")
    @GetMapping("/findByDictCode/{dictCode}")
    public Result findByDictCode(@PathVariable String dictCode){

        List<Dict> list= dictService.findByDictCode(dictCode);

        return Result.ok(list);

    }



/* 课件中的写法
    @ApiOperation(value = "根据dictCode获取下级节点")
    @GetMapping(value = "/findByDictCode/{dictCode}")
    public Result<List<Dict>> findByDictCode(
            @ApiParam(name = "dictCode", value = "节点编码", required = true)
            @PathVariable String dictCode) {
        List<Dict> list = dictService.findByDictCode(dictCode);
        return Result.ok(list);
    }*/




    // 导入字典数据
    @ApiOperation(value = "导入")
    @PostMapping("importData")
    public Result importData(MultipartFile file){

        // 调用导入的方法
        dictService.importBictData(file);

        return Result.ok();

    }


    // 导出数据字典的方式
    @ApiOperation(value = "导出Ex表格")
    @GetMapping("exportData")
    public Result exportData(HttpServletResponse response){

        dictService.exportData(response);

       return Result.ok();
    }



    // 根据数据 id 查询子数据列表
    @ApiOperation(value = "根据数据 id 查询子数据列表")
    @GetMapping("findChlidAll/{id}")
    public Result findChlidAll(@PathVariable Long id){

        List<Dict> list= dictService.findChlidAll(id);

        return  Result.ok(list);
    }












/*

    // 根据数据 id 查询子数据列表
    @ApiOperation(value = "根据数据id查询子数据列表")
    @GetMapping("findChildData/{id}")
    public Result findChildData(@PathVariable Long id) {
        List<Dict> list = dictService.findChlidData(id);
        return Result.ok(list);
    }
*/




    // 根据dictcode 和 value 查询
    @ApiOperation(value ="根据dictcode 和 value 查询")
    @GetMapping("getName/{dictCode}/{value}")
    public String getName(
            @ApiParam(name = "parentDictCode", value = "上级编码", required = true)
            @PathVariable String dictCode,

            @ApiParam(name = "value", value = "值", required = true)
            @PathVariable String value){


        String dictName=dictService.getDictName(dictCode, value);

        return  dictName;

    }
// 根据value 查询
@GetMapping("getName/{value}")
public String getName(
        @ApiParam(name = "value", value = "值", required = true)
        @PathVariable String value){
    String dictName=dictService.getDictName("", value);
    return  dictName;

}



  /*  @ApiOperation(value = "获取数据字典名称")
    @GetMapping(value = "/getName/{parentDictCode}/{value}")
    public String getName(
            @ApiParam(name = "parentDictCode", value = "上级编码", required = true)
            @PathVariable("parentDictCode") String parentDictCode,

            @ApiParam(name = "value", value = "值", required = true)
            @PathVariable("value") String value) {
        return dictService.getNameByParentDictCodeAndValue(parentDictCode, value);
    }

    @ApiOperation(value = "获取数据字典名称")
    @ApiImplicitParam(name = "value", value = "值", required = true, dataType = "Long", paramType = "path")
    @GetMapping(value = "/getName/{value}")
    public String getName(
            @ApiParam(name = "value", value = "值", required = true)
            @PathVariable("value") String value) {
        return dictService.getNameByParentDictCodeAndValue("", value);
    }*/









}
